﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using System.IO;
using Mengsk.ProxyDelivery.Domain;
using Seagull.BarTender.Print;
using Mengsk.ProxyDelivery.Utils;
using Mengsk.ProxyDelivery.App.Domain;
using Mengsk.ProxyDelivery.App.Device;
using Mengsk.ProxyDelivery.Service.Delivery;
using Com.Qiniu.ShopErp.App.Device.Print;

namespace Mengsk.ProxyDelivery.App.UI.Order
{
    /// <summary>
    /// ReturnProcessWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ReturnProcessWindowEx : System.Windows.Window
    {
        public OrderChangeViewModel OrderChange { get; set; }
        private VendorService vs = ServiceContainer.GetService<VendorService>();
        public ReturnProcessWindowEx()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbbResult.Bind<OrderChangeResultReason>();
            this.cbbResult.SelectedItem = EnumUtil.GetEnumValueDescription(this.OrderChange.OrderChange.ResultReason);
            this.tbRecivedCount.Text = this.OrderChange.OrderChange.Count.ToString();
            this.tbGoodsMoney.Text = this.OrderChange.OrderChange.GoodsMoney.ToString("F2");
            this.tbDeliveryMoney.Text = this.OrderChange.OrderChange.MoreRefundDeliveryMoney.ToString("F2");
            this.chkDelivery.IsChecked = this.OrderChange.OrderChange.RefundDeliveryMoney > 0;
            this.chkService.IsChecked = this.OrderChange.OrderChange.RefundServiceMoney > 0;
            this.tbComment.Text = this.OrderChange.OrderChange.SystemComment;
            this.tbGoodsInfo.Text = this.OrderChange.GoodsInfo;

            if (string.IsNullOrWhiteSpace(this.OrderChange.OrderChange.SystemComment))
            {
                this.tbComment.Text = "检查通过【大小码，定做,部分真皮厂家一般不退请注意】";
            }
        }

        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                if (btn == null)
                {
                    MessageBox.Show("事件源不是Button");
                    return;
                }
                var imageCtr = btn.Content as Image;
                if (imageCtr == null)
                {
                    MessageBox.Show("按钮下面不是图形控件");
                }

                Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
                bool? ret = ofd.ShowDialog();
                if (ret == null || ret.Value == false)
                {
                    return;
                }

                imageCtr.Source = (new Converters.WebUrlImageConverter()).Convert(ofd.FileName, null, null, null) as ImageSource;
                imageCtr.Tag = ofd.FileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PrintInfo()
        {
            try
            {
                PrintDevice.PrintOrderReturn(this.OrderChange.OrderChange);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {


            try
            {
                var ser = ServiceContainer.GetService<OrderChangeService>();

                if (this.cbbResult.GetSelectedEnum<Mengsk.ProxyDelivery.Domain.OrderChangeResultReason>() == ProxyDelivery.Domain.OrderChangeResultReason.None)
                {
                    MessageBox.Show("请选择退换原因");
                    return;
                }

                int count = int.Parse(this.tbRecivedCount.Text.Trim());
                if (count < 0)
                {
                    MessageBox.Show("请输入实收数量");
                    return;
                }

                float money = float.Parse(this.tbDeliveryMoney.Text.Trim());
                if (money < 0 || money > 20)
                {
                    MessageBox.Show("补助运费金额不能小于0或者不能大于20");
                    return;
                }

                string comment = this.tbComment.Text.Trim();
                if (comment.Length > 200)
                {
                    MessageBox.Show("描述字符不能大于200个字");
                    return;
                }

                string goodsInfo = this.tbGoodsInfo.Text.Trim();
                if (string.IsNullOrWhiteSpace(goodsInfo))
                {
                    throw new Exception("商品信息不能为空");
                }

                double moreRefundDeliveryMoney = double.Parse(this.tbDeliveryMoney.Text.Trim());
                double goodsMoney = double.Parse(this.tbGoodsMoney.Text.Trim());

                //检查快递发出地址，是否与系统发货地址是否匹配
                if (this.OrderChange.OrderChange.Type != OrderChangeType.ReturnWithNoOrder && this.OrderChange.OrderChange.Type != OrderChangeType.Refused && this.OrderChange.Order != null && this.OrderChange.Order.PopPayType != OrderPopPayType.COD)
                {
                    bool ret = false;
                    try
                    {
                        ret = DeliveryService.CheckAddress(this.OrderChange.OrderChange.ReturnDeliveryCompany, this.OrderChange.OrderChange.ReturnDeliveryNumber, this.OrderChange.Order.ReceiverAddress);
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show(ex.Message, "检查地址发生错误", MessageBoxButton.YesNo, MessageBoxImage.Error) != MessageBoxResult.Yes)
                        {
                            return;
                        }
                    }
                    if (ret == false)
                    {
                        if (App.LoginOperator.Rights.Contains("允许不同地址退货") == false)
                        {
                            throw new Exception("快递单号与系统发货地址区不匹配");
                        }
                        else
                        {
                            if (MessageBox.Show("快递单号与系统发货地址区不匹配，是否继续处理?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Error) != MessageBoxResult.Yes)
                            {
                                return;
                            }
                        }
                    }
                }

                ser.MarkWaitToVendor(this.OrderChange.OrderChange.Id, this.cbbResult.GetSelectedEnum<OrderChangeResultReason>(), count, comment, image1.Tag as string, image2.Tag as string, image3.Tag as string, this.chkService.IsChecked.Value, this.chkDelivery.IsChecked.Value, moreRefundDeliveryMoney, 0, goodsMoney, goodsInfo);
                if (this.chkPrintInfo.IsChecked != null && this.chkPrintInfo.IsChecked.Value)
                {
                    this.PrintInfo();
                }
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
